//
//  JFSegmentControl.m
//  SLAppProduct
//
//  Created by AlanWang on 2018/3/14.
//  Copyright © 2018年 Sunline. All rights reserved.
//

#import "JFSegmentControl.h"
#import <JFBase/JFBase.h>

#define ANIMATION_VIEW_HEIGHT 3.f
static NSInteger kTagAlpha = 1000;

@interface JFSegmentControl()

@property (nonatomic, strong) UIView *animateView;

@end

@implementation JFSegmentControl

- (instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray *)titleArray
{
    self = [super initWithFrame:frame];
    if (self) {
        _titleArray = titleArray;
        
        [self setup];
    }
    return self;
}

- (void)setup{
    NSInteger count = self.titleArray.count;
    CGFloat width = self.frame.size.width / count;
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i < count; i++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(width * i, 0, width, CGRectGetHeight(self.frame));
        button.tag = i+kTagAlpha;
        [button setTitle:self.titleArray[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitleColor:AWThemeColorForKey(@"Market.titleColor") forState:UIControlStateNormal];
        [button setTitleColor:UIColorFromHex(0xFC724C) forState:UIControlStateSelected];
        [button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        button.backgroundColor = AWThemeColorForKey(@"Global.navBgColor");
        [self addSubview:button];
        
        [array addObject:button];
    }
    _itemArray = [NSArray arrayWithArray:array];
    
    _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame) - 1, self.frame.size.width, 1)];
    _lineView.backgroundColor = AWThemeColorForKey(@"Market.separationColor");
    [self addSubview:_lineView];
    
    self.animateView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame) - ANIMATION_VIEW_HEIGHT, 15,ANIMATION_VIEW_HEIGHT)];
    _animateView.backgroundColor = [UIColor colorWithRed:249/255. green:90/255. blue:41/255. alpha:1];
    [self addSubview:_animateView];
    
    self.selectIndex = 0;
}

- (void)setTitleArray:(NSArray *)titleArray {
    _titleArray = titleArray;
    
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    
    [self setup];
}

- (void)setSelectIndex:(NSInteger)selectIndex {
    _selectIndex = selectIndex;
    
    for (int i = 0; i < self.titleArray.count; i++) {
        UIButton *button = (UIButton *)[self viewWithTag:i+kTagAlpha];
        if (i == selectIndex) {
            button.selected = YES;
        }else {
            button.selected = NO;
        }
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        CGFloat width = self.frame.size.width / self.titleArray.count;
        self.animateView.frame = CGRectMake(width * selectIndex + (width - 15) / 2, CGRectGetHeight(self.frame) - ANIMATION_VIEW_HEIGHT, 15,ANIMATION_VIEW_HEIGHT);
    }];
}

- (void)clickAction:(UIButton *)button {
    NSInteger index = button.tag - kTagAlpha;
    if (_delegate && [_delegate respondsToSelector:@selector(selectControlDidSelectedAtIndex:)]) {
        [_delegate selectControlDidSelectedAtIndex:index];
    }
    
    if (_delegate && [_delegate respondsToSelector:@selector(willDidSelectedAtIndex:)]) {
        if (![_delegate willDidSelectedAtIndex:index]) {
            return;
        }
    }
    
    self.selectIndex = index;
}

@end
